package com.itheima.servicce;

import com.itheima.mapper.userMapper;
import com.itheima.pojo.user;
import com.itheima.utile.SessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

public class userService {
    //链接数据库
   static SqlSession session = SessionFactoryUtils.getSession();
   static userMapper userMapper = session.getMapper(userMapper.class);


    /**
     * 验证用户名是否被注册
     * @return true 表示用户名未注册
     *          false 表示用户名已存在
     */
    public boolean checkUserName(String userName){

        user user = userMapper.selectUserName(userName);

        //session.close();

        if(user!=null&&user.getUserName().equals(userName)){
            return false;//用户名已存在
        }else {
            return true;//用户名未注册
        }

    }

    /**
     * 添加用户到数据库中
     * @return
     */
    public boolean adduser(user user){
        Integer i = userMapper.addUser(user);
        if(i>0){
            session.commit();
            return true;//注册成功
        }else {
            return false;//注册失败
        }

    }
}
